package com.hit.RuningGirl.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.hit.RuningGirl.main.Yelling;

import static com.hit.RuningGirl.main.Yelling.ViewPort_HEIGHT;
import static com.hit.RuningGirl.main.Yelling.ViewPort_WIDTH;
import static com.hit.RuningGirl.main.Yelling.assetManager;


public class MainScreen extends YellScreen {

	public Stage getStage() {
		return stage;
	}

	public Stage stage;

	private StretchViewport viewport;

	private Image imageStarCount;

	private Image[] images;

	private Image startimage;

	private Image initimage;

	private Texture initbg, startbg;

	static  Label label1;

	public static boolean Debug = true;

	public MainScreen(Yelling game) {
		super(game);
		init();
	}

	public void init() {

		viewport = new StretchViewport(ViewPort_WIDTH, ViewPort_HEIGHT);

		stage = new Stage(viewport);

		initbg = assetManager.get("images/start.png");

		startbg = assetManager.get("images/start.png");

		startimage = new Image(startbg);

		initimage = new Image(initbg);

		initimage.setSize(ViewPort_WIDTH, ViewPort_HEIGHT);


//		Texture now = new Texture(Gdx.files.internal("images/init.png"));
//
//		TextureRegion[] nowr = new TextureRegion[3];
//		nowr[0] = new TextureRegion(now,98,324,110,107);
//		nowr[1] = new TextureRegion(now,306,324,110,107);
//		nowr[2] = new TextureRegion(now,506,324,110,107);


		AddLabel(getLabel(0));
		//设置星星计数器的图片
		Texture starUI = new Texture(Gdx.files.internal("images/starUI.png"));

		Texture start = new Texture(Gdx.files.internal("images/buttonStart.png"));
		Texture end = new Texture(Gdx.files.internal("images/buttonEnd.png"));
		TextureRegion[] nowr = new TextureRegion[3];
		nowr[0] = new TextureRegion(start,0,0,192,64);
		nowr[1] = new TextureRegion(end,0,0,192,64);
		nowr[2] =new TextureRegion(starUI,0,0,55,55);
		images = new Image[3];
		for (int i = 0 ; i < 3 ; i ++){
			images[i] = new Image(nowr[i]);
		}


		images[0].setPosition(280, 150);

		images[1].setPosition(280, 80);

		images[2].setPosition(0, 390);


		//设置星星计数器



		initListener();

		Gdx.input.setInputProcessor(stage);


	}

	public void AddLabel(Label label1) {

		stage.addActor(label1);
		stage.act();
		stage.draw();
	}

	public Label getLabel(int i)
	{
		BitmapFont font;
		font = new BitmapFont(Gdx.files.internal("45.fnt"));
		Label.LabelStyle style;
		style = new Label.LabelStyle(font, font.getColor());
		// 实例化标签1
		label1 = new Label("×"+i, style);

		label1.setPosition(60, 400);

		label1.setFontScale(2);

		label1.setColor(Color.GREEN);

		return  label1;
	}

	@Override
	public void render(float delta) {

		Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);

		update(delta);
		stage.addActor(label1);
		stage.act();

		stage.draw();

	}

	private void initListener() {

		//start界面监听函数
		startimage.addListener(new ClickListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y,
									 int pointer, int button) {

				Debug = false;

				Sound s = assetManager.get("audio/jiaosheng.wav", Sound.class);
				s.play();

				return true;
			}
		});

		images[0].addListener(new ClickListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y,
									 int pointer, int button) {

				Sound s = assetManager.get("audio/select.wav");

				s.play();

				//游戏背景音
				Music m = assetManager.get("audio/schnappi.mp3");
				m.play();


				GameScreen.level = 0;

				GameScreen g = new GameScreen(game);

				game.setScreen(g);

				return true;
			}
		});
		images[1].addListener(new ClickListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y,
									 int pointer, int button) {
				Sound s = assetManager.get("audio/select.wav");
				s.play();

//				GameScreen.level = 1;
//				GameScreen g = new GameScreen(game);
//				game.setScreen(g);
				return true;
			}
		});
		images[2].addListener(new ClickListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y,
									 int pointer, int button) {
				Sound s = assetManager.get("audio/select.wav");
				s.play();
//				GameScreen.level = 2;
//				GameScreen g = new GameScreen(game);
//				game.setScreen(g);
				return true;
			}
		});

	}

	@Override
	public void hide() {

		stage.clear();

		stage.dispose();
	}

	@Override
	public void handleInput() {
	}

	@Override
	public void update(float dt) {

		if (Debug) {

			stage.getActors().clear();
			stage.addActor(startimage);
			stage.addActor(label1);

		} else {

			stage.getActors().clear();
			stage.addActor(initimage);
			stage.addActor(label1);

			for (int i = 0; i < images.length; i++) {
				stage.addActor(images[i]);
			}
		}
	}

}
